package com.google.hackathon.geochallenge;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONException;
import org.json.JSONObject;

public class Checkpoint implements Parcelable {
    private final double mLatitude;
    private final double mLongitude;
    private final double mAccuracy;

    public Checkpoint(double latitude, double longitude, double accuracy) {
        mLatitude = latitude;
        mLongitude = longitude;
        mAccuracy = accuracy;
    }

    public Checkpoint(Parcel in) {
        mLatitude = in.readDouble();
        mLongitude = in.readDouble();
        mAccuracy = in.readDouble();
    }

    public Checkpoint(JSONObject json) throws JSONException {
        mLatitude = json.getDouble("latitude");
        mLongitude = json.getDouble("longitude");
        mAccuracy = json.getDouble("accuracy");
    }

    public double getLatitude() {
        return mLatitude;
    }

    public double getLongitude() {
        return mLongitude;
    }

    public double getAccuracy() { return mAccuracy; }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeDouble(mLatitude);
        out.writeDouble(mLongitude);
        out.writeDouble(mAccuracy);
    }

    public JSONObject toJSON() throws JSONException {
        JSONObject json = new JSONObject();
        json.put("latitude", mLatitude);
        json.put("longitude", mLongitude);
        json.put("accuracy", mAccuracy);
        return json;
    }

    public static final Parcelable.Creator<Checkpoint> CREATOR
            = new Parcelable.Creator<Checkpoint>() {
        public Checkpoint createFromParcel(Parcel in) {
            return new Checkpoint(in);
        }

        public Checkpoint[] newArray(int size) {
            return new Checkpoint[size];
        }
    };
}
